﻿using MainWebService;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace MainWebService
{
    public class QuestionOperations
    {

        internal string AddQuestion(Questions objQues)
        {
            object messgae = null;
            object QuestionId = null;
            try
            {
                DAO d = new DAO();
                List<SqlParameter> param = new List<SqlParameter>();
                param.Add(d.AddParameter("@QuestionText", objQues.QuestionText));
                param.Add(d.AddParameter("@QuestionType", objQues.QuestionType));
                param.Add(d.AddParameter("@IsNumeric", objQues.IsNumeric));
                param.Add(d.AddParameter("@UserId", objQues.UserId));
                param.Add(d.AddParameter("@QuestionSubType", objQues.QuestionSubType));
                if (objQues.AnswerOptions != null && objQues.AnswerOptions.Count > 0)
                {
                    string csv = String.Join(",", objQues.AnswerOptions.Select(x => x.ToString()).ToArray());
                    param.Add(d.AddParameter("@AnswerOptions", csv));
                }
                param.Add(d.AddParameter("@QuestionId", objQues.QuestionId, SqlDbType.Int, 10, ParameterDirection.InputOutput));
                param.Add(d.AddParameter("@Messages", messgae, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
                d.ExecuteQuery("P_AddQuestion", param, ref QuestionId, ref messgae);

            }
            catch (Exception ex)
            {
                messgae = ex.Message.ToString();
            }
            return messgae.ToString();
        }

        internal List<Questions> GetQuestions(int clientId, string hashCode)
        {
            List<Questions> lstQuestions = new List<Questions>();
            Questions qDetails = null;
            DataSet ds = new DataSet();
            DataTable dtService = null;
            DataRow dr = null;
            DAO d = new DAO();
            int itemId = 0;
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@ClientId", clientId));
            param.Add(d.AddParameter("@HashCode", hashCode));
            ds = d.ExecuteDataSet("P_ListQuestions", param);
            if (Utility.IsValidDataSet(ds))
            {
                if (Utility.IsValidDataSet(ds, 0))
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        qDetails = new Questions();
                        qDetails.QuestionText = Convert.ToString(ds.Tables[0].Rows[i]["Question"]);
                        qDetails.QuestionId = Convert.ToInt32(ds.Tables[0].Rows[i]["QuestionId"]);
                        qDetails.CreateDate = Convert.ToString(ds.Tables[0].Rows[i]["CreateDate"]);
                        qDetails.QuestionType = Convert.ToString(ds.Tables[0].Rows[i]["QuestionType"]);
                        qDetails.QuestionSubType = Utility.GetString(ds.Tables[0].Rows[i]["QuestionSubType"]);
                        qDetails.AnswerOptions = Convert.ToString(ds.Tables[0].Rows[i]["Options"]).Split(',').ToList<string>();
                        string[] itemIds = Convert.ToString(ds.Tables[0].Rows[i]["ItemIds"]).Split(',');
                        string[] serviceNames = Convert.ToString(ds.Tables[0].Rows[i]["ServiceName"]).Split(',');
                        dtService = new DataTable();
                        dtService.Columns.Add(new DataColumn("Id"));
                        dtService.Columns.Add(new DataColumn("Name"));
                        for (int j = 0; j < itemIds.Length; j++)
                        {

                            if (Int32.TryParse(itemIds[j], out itemId) && itemId > 0)
                            {
                                dr = dtService.NewRow();
                                dr[0] = itemId;
                                dr[1] = serviceNames[j] != null ? Convert.ToString(serviceNames[j]) : "";
                                dtService.Rows.Add(dr);
                            }
                        }

                        qDetails.ServicesList = new DataSet();
                        qDetails.ServicesList.Tables.Add(dtService);
                        lstQuestions.Add(qDetails);
                    }
                }
                else
                {
                    qDetails = new Questions();
                    lstQuestions.Add(qDetails);
                }
            }
            else
            {
                qDetails = new Questions();
                lstQuestions.Add(qDetails);
            }


            return lstQuestions;
        }

        internal string AssociateQuestion(Questions question,int serviceId)
        {
            try
            {
                DAO d = new DAO();
                List<SqlParameter> param = new List<SqlParameter>();
                param.Add(d.AddParameter("@ServiceId", serviceId));
                param.Add(d.AddParameter("@QuestionId", question.QuestionId));
                param.Add(d.AddParameter("@IsBeforSubmittingScan", question.IsBeforSubmittingScan));
                param.Add(d.AddParameter("@IsAfterSubmittingScan", question.IsAfterSubmittingScan));
                param.Add(d.AddParameter("@IsSubmittingValidScan", question.IsSubmittingValidScan));
                param.Add(d.AddParameter("@IsSubmittingInValidScan", question.IsSubmittingInValidScan));
                d.ExecuteQuery("P_AssociateQuestionToService", param);
                return "0";
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
            
        }
    }
}